/*
 * @Author: Wangjun
 * @Date: 2023-08-14 10:40:47
 * @LastEditTime: 2023-11-30 17:32:32
 * @LastEditors: Wangjun
 * @Description:
 * @FilePath: \xr_node_calc\noderdb\op_magus_ai.go
 * hnxr
 */
package noderdb

import (
	"errors"

	"gitee.com/haodreams/godriver/opdb"
	"gitee.com/hnxr_1/nodex"
)

func init() {
	nodex.Reister(new(MagusAI))
}

type MagusAI struct {
	nodex.Node
	item *opdb.Real
	Y    nodex.Double `remark:"输出Y,,out"`
	Tag  string       //标签
	Kind string       //类别
}

func (m *MagusAI) GetModelID() int {
	return nodex.ModIn
}

func (m *MagusAI) GetName() string {
	return "MagusAI"
}

func (m *MagusAI) Init(mem *nodex.Memory) (err error) {
	extends := m.GetExtends()
	atag := extends["tag"]
	tag, _ := atag.(string)
	// if ok {
	// 	m.Tag = tag
	// 	m.item = GetItem(parent.Category, parent.RID, tag)
	// }
	acategory := extends["category"]
	category, _ := acategory.(string)
	if tag != "" {
		m.Tag = tag
		m.Kind = category
		m.item = dataitem.GetOPValue(tag, true)
	}

	return
}

/**
 * @description: 节点信息
 * @return {*}
 */
func (m *MagusAI) RegData() []byte {
	return nodex.MakeRegData(m, "麦杰实时库模拟量输入", m.GetName(), "")
}

func (m *MagusAI) Calc() (err error) {
	if m.item == nil {
		m.Bad = true
		m.Err = errors.New(m.Tag + ":测点不存在")
		if m.EnQ {
			return m.Err
		}
		return
	}
	if m.item.Status < 0 {
		m.Err = errors.New(m.Tag + ":测点超时")
		m.Bad = true
		if m.EnQ {
			return m.Err
		}
		return
	}
	m.Y.Value = m.item.Value
	m.Bad = false
	return
}
